package com.mjlf.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;

/**
 * @ClassName GatherChannelTest
 * @auther mjlf
 * @Date 2019/9/11 9:46
 * @Description TODO
 */
public class GatherChannelTest {

    public static void main(String[] args) throws IOException {
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(10);
        byteBuffer1.put("abc".getBytes());
        byteBuffer1.flip();

        ByteBuffer byteBuffer2 = ByteBuffer.allocate(10);
        byteBuffer2.put("def".getBytes());
        byteBuffer2.flip();

        ByteBuffer byteBuffer3 = ByteBuffer.allocate(10);
        byteBuffer3.put("ghi".getBytes());
        byteBuffer3.flip();

        ByteBuffer byteBuffer4 = ByteBuffer.allocate(10);
        byteBuffer4.put("jkl".getBytes());
        byteBuffer4.flip();

        ByteBuffer[] byteBuffers = new ByteBuffer[]{byteBuffer1, byteBuffer2, byteBuffer3, byteBuffer4};

        FileOutputStream fileOutputStream = new FileOutputStream("blala.txt");
        GatheringByteChannel gatheringByteChannel = fileOutputStream.getChannel();

        while (gatheringByteChannel.write(byteBuffers) > 0){

        }

        fileOutputStream.close();
        gatheringByteChannel.close();
    }
}
